<?php
/**
 * 导航模型
 * 
 * @version $Id: Nav.php 482 2012-11-05 06:50:02Z linsir123 $
 */
class modelNav
{
	/**
	 * @var TOP数据请求对象
	 */
	public $top;
	
	/**
	 * @var 分类数据(缓存)
	 */
	public $data = array();
	
	/**
	 * @var 导航使用的分类数据
	 */
	public $locations = array();
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		$this->top = M::topRequest();
	}
	
	/**
	 * 返回二级导航数据
	 */
	public function getSub($pId)
	{
		$data = array();
		$tmp = $this->top->itemcatsGet(null, $pId);
		$this->_combineData($tmp);
		foreach ($tmp as $v) 
			$data[$v['id']] = $v['title'];
			
		///
		return $data;
	}
	
	/**
	 * 根据传入的ID取得分类名称
	 */
	public function getOne($cId, $pinyin = "")
	{
		$data = isset($this->data[$cId])?$this->data[$cId]:null;
		
		///
		if (empty($data)) {
			$tmp = $this->top->itemcatsGet($cId);
			$this->_combineData($tmp);
			if ( ! empty($tmp)) 
				$data = $tmp[0];
		}
		
		///
		if ($data && $pinyin) 
			return $data['pinyin']==$pinyin?$data:null;
			
		///
		return $data;
	}
	
	/**
	 * 根据传入的ID取得分类名称
	 */
	public function getLocation($cId, $type = 'list')
	{
		$location = null;
		if ( ! empty($cId) && $nav = $this->getOne($cId)) {
			$this->locations[] = $nav;
			if ($nav['pid']) 
				$location .= $this->getLocation($nav['pid'], $type);
				
			///
			$href = Route::urlClass(array('cid'=>$nav['id'], 'type'=>$type));
			$location .= ' <a href="'. $href .'">'. $nav['title'] .'</a> &gt;';
		}
		///
		return $location;
	}
	
	/**
	 * 取和天猫品牌分类列表
	 */
	public function getTmallAll()
	{
		$list = array();
		$tmp = $this->top->tmallTemaiSubcatsSearch();
		foreach ($tmp as $v) 
			$list[$v['id']] = $v;
			
		///
		return $list;
	}
	
	/**
	 * 取和天猫品牌分类
	 */
	public function getTmallOne($cId)
	{
		$list = $this->getTmallAll();
		///
		return isset($list[$cId])?$list[$cId]:null;
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 合并数据
	 */
	private function _combineData($data)
	{
		if (is_array($data)) {
			foreach ($data as $v) 
				$this->data[$v['id']] = $v;
		}
	}
}
